class Solution {
public:
    int numBusesToDestination(vector<vector<int>>& routes, int source, int target) {
        if (source == target) return 0;
        vector<set<int> > vv(25005);
        for (auto ve : routes)
        {
            for (int i = 0; i < ve.size(); i++) {
                for (int j = 0; j < ve.size(); j++) {
                    if (i != j)
                        vv[ve[i]].insert(ve[j]);
                }
            }
        }
        vector<int> m(10005, 0);
        int len = 0;
        stack<int> s1, s2;
        s1.push(source);
        while (s1.size()) {
            len++;
            while (s1.size())
            {
                int i = s1.top(); s1.pop();
                for (auto e : vv[i])
                {
                    if (e != source && m[e] == 0)
                    {
                        m[e] = len;
                        s2.push(e);
                        if (e == target) {
                            return len;
                        }
                    }
                }

            }
            swap(s1, s2);
        }
        return -1;
    }
};